package cn.earl.springboot.user.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class MobileCheck {

	private static Logger logger = Logger.getLogger(MobileCheck.class);
	
	/**
	 * 返回 '0' 视为发送成功，其他内容为错误提示内容 30：密码错误<br>
	 * 40：账号不存在<br>
	 * 41：余额不足<br>
	 * 42：帐号过期<br>
	 * 43：IP地址限制<br>
	 * 50：内容含有敏感词<br>
	 * 51：手机号码不正确
	 * 
	 * @param number
	 * @return 第一个是发送的状态码，第二个是验证码
	 * @throws IOException
	 */
	public static List sendMessage(String number) throws IOException {
		// 返回值
		List<String> list = new ArrayList<>();

		// ----------------------------------------------------------------------------
		// 短信宝
		String httpUrl = "http://api.smsbao.com/sms";
		String result = null;
		// 接口参数
		String username = "17602089224";
		String password = "951166656";
		// 随机验证码
		int code = (int) (Math.random() * 1000000);
		// 发送的消息
		String content = "【youzhu】验证码：" + code + "，五分钟之内有效";

		// 连接参数
		StringBuffer httpArg = new StringBuffer();
		httpArg.append("u=").append(username).append("&");
		httpArg.append("p=").append(md5(password)).append("&");
		httpArg.append("m=").append(number).append("&");
		httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));

		httpUrl += "?"+httpArg.toString();

		// 定位
		URL url = new URL(httpUrl);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		connection.connect();
		// 得到响应的数据
		InputStream inputStream = connection.getInputStream();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		// 将数据写出到data数组中
		while ((len = inputStream.read(data)) != -1) {
			byteArrayOutputStream.write(data, 0, len);
		}
		result = new String(byteArrayOutputStream.toByteArray(), "utf-8");
		inputStream.close();
		// ----------------------------------------------------------------------------
		list.add(result);
		list.add(code + "");
		logger.info(number+" had successily send the message");
		return list;
	}

	public static String md5(String plainText) {
		StringBuffer buf = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return buf.toString();
	}

	public static String encodeUrlString(String str, String charset) {
		String strret = null;
		if (str == null)
			return str;
		try {
			strret = java.net.URLEncoder.encode(str, charset);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return strret;
	}
}
